package com.maila.calltaxi_driver.ui;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.maila.calltaxi_driver.R;
import com.maila.calltaxi_driver.utils.RegularUtils;
import com.maila.calltaxi_driver.utils.ToastUtil;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/4.
 * 重置密码
 */

public class ForgetPwdActivity extends AppCompatActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.title_back)
    LinearLayout titleBack;
    @BindView(R.id.title)
    AutoRelativeLayout title;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.bt_getcode)
    Button btGetcode;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.bt_commit)
    Button btCommit;
    @BindView(R.id.registerlayout)
    AutoLinearLayout registerlayout;
    boolean phoneok;
    boolean codeok;
    boolean pwdok;
    String phone;
    String code;
    String pwd;
    private boolean isSendSuccess;
    private Timer mTimer;
    private Handler mHandler = new Handler() {

        public void handleMessage(android.os.Message msg) {
            if (msg.what == 0) {
                isSendSuccess = false;
                etCode.setText("");
                btGetcode.setEnabled(true);
                btGetcode.setClickable(true);
                btGetcode.setBackgroundResource(R.drawable.tv_bg_cz_gree);
                btGetcode.setText("重新发送");
                if (mTimer != null) {
                    mTimer.cancel();
                    mTimer = null;
                }
            } else {
                btGetcode.setEnabled(false);
                btGetcode.setBackgroundResource(R.drawable.tv_bg_cz_hui);
                btGetcode.setText(msg.what + "秒");
            }
        }
    };
    private int countdown=60;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.forgetpwd_activity);
        ButterKnife.bind(this);
        setListener();

    }

    private void setListener() {
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (RegularUtils.isPhoneNumber(s.toString())) {
                    phoneok = true;
                    phone = s.toString();
                } else {
                    phoneok = false;
                    phone = "";
                }
                updatabtnstatus();
            }
        });

        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 3) {
                    code = s.toString();
                    codeok = true;
                } else {
                    codeok = false;
                    code = "";
                }
                updatabtnstatus();
            }
        });
        etPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 6) {
                    pwd = s.toString();
                    pwdok = true;
                } else {
                    pwd = "";
                    pwdok = false;
                }
                updatabtnstatus();
            }
        });
    }

    private void updatabtnstatus() {
        if(phoneok&codeok&pwdok){
            btCommit.setClickable(true);
            btCommit.setBackgroundResource(R.drawable.tv_bg_cz_gree);
        }else {
            btCommit.setClickable(false);
            btCommit.setBackgroundResource(R.drawable.tv_bg_cz_hui);
        }
    }

    @OnClick({R.id.title_back, R.id.bt_getcode, R.id.bt_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.bt_getcode:
                if(phoneok){
                    btGetcode.setClickable(false);
                    countdown = 60;
                    mTimer = new Timer();
                    mTimer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            if (countdown >= 0) {
                                mHandler.sendEmptyMessage(countdown);
                                countdown--;
                            }
                        }
                    }, 0, 1000);
                    ToastUtil.show(this,"已向"+phone+"发送短信，请稍等");
                }else {
                    ToastUtil.show(this,"请先填写正确的手机号");
                }

                break;
            case R.id.bt_commit:
                if(phoneok){
                    if(codeok){
                        if(pwdok){
                            ToastUtil.show(this,"修改成功");
                            finish();
                        }else {
                            ToastUtil.show(this,"请填写6位数字密码");
                        }
                    }else {
                        ToastUtil.show(this,"请填写正确的验证码");
                    }
                }else {
                    ToastUtil.show(this,"请填写正确的手机号");
                }
                break;
        }
    }
}
